import $service from '../../../../service'
const app = getApp()
let timer = null
let info = {}

Page({
  data: {
    codeText: "60s后可重新获取",
    disable: true,
    authCode: '',
    phone: ''
  },
  onLoad(e) {
    info = e.info ? JSON.parse(e.info) : {}
    this.setData({
      phone: info.mobile.replace(/(\d{3})\d{4}(\d{4})/, "$1****$2")
    })
    this.count()
  },
  count() {
    let time = 60
    this.data.codeText = '60s后可重新获取'
    timer = setInterval(() => {
      time--
      if (time == 0) {
        clearInterval(timer)
        this.data.codeText = '重新获取'
        this.data.disable = false
      }  else {
        this.data.codeText = `${time}s后可重新获取`
        this.data.disable = true
      }
      this.setData({
        codeText: this.data.codeText,
        disable:  this.data.disable 
      })
    }, 1000)
  },
  getCode() {
    if (this.data.codeText == '重新获取') {
      app.http.individualTelecom3Factors(info).then(res => {
        info.flowId = res.data.flowId
        this.count()
      }).catch(err => {
        $service.showToast(err.msg)
      })
    }
  },
  inputChange(e) {
    let {value} = e.detail
    this.setData({
      authCode: value
    })
  },
  sure() {
    app.http.qryPersonSeals({
      authCode: this.data.authCode,
      flowId: info.flowId
    }).then(res => {
      this.getDoctorInfo()
      wx.reLaunch({
          url: '../../../doctor_side/index/index'
      })
    }).catch(err => {
      $service.showToast(err.msg)
    })
  },
  // 更新缓存医生个人信息
  getDoctorInfo() {
    app.http.getDoctorInfo().then(res => {
        $service.setStore('doctorInfo', JSON.stringify(res.data));
    })
  },
})